var RPS = artifacts.require("./RPS.sol");

contract('RPS', function(accounts) {
	
  	/*it("event listen", function() {  		
    	RPS.deployed().then(function(instance) {
    		var createEvent = instance.Create();
    		createEvent.watch(function (error, result) {
      			if (error) {
        			console.log("Error: " + error);
      			} else {
        			console.log("player: " + result.args.player);
        			console.log("hashId: " + result.args.hashId);
        			console.log("stake: " + result.args.stake);
      			}
      			createEvent.stopWatching();
    		});

      		console.log("create()");
      		var trxId = instance.create({from: accounts[0], value: 0x10});
      		console.log("returns: " + trxId);
    	})
  	});*/
  
  	it("flow", function() {  		
  		var rps;
  		var hashId;
  		var blindedBid1;
  		var blindedBid2;
  		var choice1 = 'r';
  		var choice2 = 'p';
  		
  		function printLog(log)
		{
    		if (log.event == "Debug") {
    			console.log("\tDebug Event");
    			console.log("\t----------------------------------------------");
    			console.log("\tplayer1: " + log.args.player1);
    			console.log("\tplayer2: " + log.args.player2);
    			console.log("\tblindedBid1: " + log.args.blindedBid1);
    			console.log("\tblindedBid2: " + log.args.blindedBid2);
    			console.log("\tchoice1: " + log.args.choice1);
    			console.log("\tchoice2: " + log.args.choice2);
    			console.log("\tstake: " + log.args.stake);
    			console.log("\twinner: " + log.args.winner);
    			console.log("\tcreateTime: " + log.args.createTime);
    			console.log("\tbiddingStartTime: " + log.args.biddingStartTime);
    			console.log("\trevealStartTime: " + log.args.revealStartTime);
    			console.log("\tstate: " + log.args.state);
    		}
    		else if (log.event == "Create") {
    			console.log("\tCreate Event");
    			console.log("\t----------------------------------------------");
    			console.log("\tplayer: " + log.args.player);
    			console.log("\thashId: " + log.args.hashId);
    			console.log("\tstake: " + log.args.stake);
    		}
    		else if (log.event == "Destroy") {
    			console.log("\tDestroy Event");
    			console.log("\t----------------------------------------------");
    			console.log("\tplayer: " + log.args.player);
    			console.log("\thashId: " + log.args.hashId);
    			console.log("\tstake: " + log.args.stake);
    		}
     		else if (log.event == "Challenge") {
    			console.log("\tChallenge Event");
    			console.log("\t----------------------------------------------");
    			console.log("\tplayer: " + log.args.player);
    			console.log("\thashId: " + log.args.hashId);
    			console.log("\tstake: " + log.args.stake);
    		}
     		else if (log.event == "BlindedBid") {
    			console.log("\tBlindedBid Event");
    			console.log("\t----------------------------------------------");
    			console.log("\tplayer: " + log.args.player);
    			console.log("\tblindedBid: " + log.args.blindedBid);
    		}
     		else if (log.event == "Bid") {
    			console.log("\tBid Event");
    			console.log("\t----------------------------------------------");
    			console.log("\tplayer: " + log.args.player);
    			console.log("\thashId: " + log.args.hashId);
    			console.log("\tblindedBid: " + log.args.blindedBid);
    		}
     		else if (log.event == "Reveal") {
    			console.log("\tReveal Event");
    			console.log("\t----------------------------------------------");
    			console.log("\tplayer: " + log.args.player);
    			console.log("\thashId: " + log.args.hashId);
    			console.log("\tchoice: " + log.args.choice);
    			console.log("\tsecret: " + log.args.secret);
    			console.log("\tok: " + log.args.ok);
    		}
     		else if (log.event == "CheckAndSettle") {
    			console.log("\tCheckAndSettle Event");
    			console.log("\t----------------------------------------------");
    			console.log("\tplayer: " + log.args.player);
    			console.log("\thashId: " + log.args.hashId);
    			console.log("\tresult: " + log.args.result);
    		}
     		else if (log.event == "Settle") {
    			console.log("\tSettle Event");
    			console.log("\t----------------------------------------------");
    			console.log("\tplayer: " + log.args.player);
    			console.log("\thashId: " + log.args.hashId);
    			console.log("\tstake: " + log.args.stake);
    		}
    		else {
    			console.log("\tUnknown Event: " + log.event);
    		}
		}
  		
    	RPS.deployed().then(function(instance) {
			rps = instance;			
      		console.log("create()");
      		return rps.create({from: accounts[0], value: 0x10});
      	})
      	
      	/*.then(function(result) {
			for (var i = 0; i < result.logs.length; i++) {
        		var log = result.logs[i];
        		if (log.event == "Create") {
        			hashId = log.args.hashId;
        		}
    		}
      		console.log("destroy(): " + hashId);
   			return rps.destroy(hashId, {from: accounts[0]});
    	})*/

      	.then(function(result) {
			for (var i = 0; i < result.logs.length; i++) {
        		var log = result.logs[i];
        		printLog(log);
        		if (log.event == "Create") {
        			hashId = log.args.hashId;
        		}
    		}
      		console.log("challenge()");
   			return rps.challenge(hashId, {from: accounts[1], value: 0x10});
    	})
    	
    	.then(function(result) {
			for (var i = 0; i < result.logs.length; i++) {
        		var log = result.logs[i];
        		printLog(log);
    		}
      		console.log("generateBlindedBid1()");
    		return rps.generateBlindedBid(choice1, "123456", {from: accounts[0]});
    	})
    	
    	.then(function(result) {
			for (var i = 0; i < result.logs.length; i++) {
        		var log = result.logs[i];
        		printLog(log);
        		if (log.event == "BlindedBid") {
        			blindedBid1 = log.args.blindedBid;
        		}
    		}
      		console.log("bid1()");
   			return rps.bid(hashId, blindedBid1, {from: accounts[0]});
    	})

    	.then(function(result) {
			for (var i = 0; i < result.logs.length; i++) {
        		var log = result.logs[i];
        		printLog(log);
        		if (log.event == "Bid") {
        		}
    		}
      		console.log("generateBlindedBid2()");
    		return rps.generateBlindedBid(choice2, "123456");
    	})

    	.then(function(result) {
			for (var i = 0; i < result.logs.length; i++) {
        		var log = result.logs[i];
        		printLog(log);
        		if (log.event == "BlindedBid") {
        			blindedBid2 = log.args.blindedBid;
        		}
    		}
      		console.log("bid2()");
   			return rps.bid(hashId, blindedBid2, {from: accounts[1]});
    	})

    	.then(function(result) {
			for (var i = 0; i < result.logs.length; i++) {
        		var log = result.logs[i];
        		printLog(log);
        		if (log.event == "Bid") {
        		}
    		}
      		console.log("reveal1()");
   			return rps.reveal(hashId, choice1, "123456", {from: accounts[0]});
    	})
    	
    	.then(function(result) {
			for (var i = 0; i < result.logs.length; i++) {
        		var log = result.logs[i];
        		printLog(log);
        		if (log.event == "Reveal") {
        			console.log("ok: " + log.args.ok);
        		}
    		}
      		console.log("reveal2()");
   			return rps.reveal(hashId, choice2, "123456", {from: accounts[1]});
    		
    	})
    	
    	.then(function(result) {
			for (var i = 0; i < result.logs.length; i++) {
        		var log = result.logs[i];
        		printLog(log);
        		if (log.event == "Reveal") {
        		}
    		}
      		console.log("checkAndSettle1()");
   			return rps.checkAndSettle(hashId, {from: accounts[0]});
    	})
    	
    	.then(function(result) {
			for (var i = 0; i < result.logs.length; i++) {
        		var log = result.logs[i];
       			printLog(log);
    		}
      		console.log("checkAndSettle2()");
   			return rps.checkAndSettle(hashId, {from: accounts[1]});
    	})

    	.then(function(result) {
			for (var i = 0; i < result.logs.length; i++) {
        		var log = result.logs[i];
       			printLog(log);
    		}

    		var contractBalance = web3.eth.getBalance(rps.address).toNumber();
    		console.log("contractBalance: " + contractBalance);
    	})

    	.catch(function(err){
    		console.log(err);
		});

      	console.log("End");
  	});  
});
